package server;


import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.HashMap;

public class SVGWriter {
    
    final static int WIDTH = 2400;
    final static int HEIGHT = 500;
    final static int POS_X_PROCESS_START = 100;
    final static int SIZE_TIME_UNIT = 30;
    
    protected PrintWriter out;
    protected HashMap<Integer, Processus> listeProcessus = new HashMap<Integer, Processus>();
    
    public SVGWriter(String filename) {
        
        try {
          out = new PrintWriter(new BufferedWriter(new FileWriter(filename)));
        }
        catch(Exception e){
          e.printStackTrace();
        }
          
        out.println("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" + "\n"
                  + "<svg xmlns=\"http://www.w3.org/2000/svg\" height=\""+HEIGHT+"\" width=\""+WIDTH+"\">" + "\n"
                  + "  <defs>" + "\n"
                  + "    <marker id=\"Triangle\" viewBox=\"0 0 40 40\" refX=\"16\" refY=\"16\"" + "\n"
                  + "     markerWidth=\"10\" markerHeight=\"10\" orient=\"auto\">" + "\n"
                  + "       <path d=\"M 0 0 L 16 16 L 0 32 L 40 16 Z\" />" + "\n"
                  + "    </marker>" + "\n"
                  + "  </defs>");
    }

    /**
     * Ecrit puis ferme le fichier
     */
    public void close() {
        out.println("</svg>");
        out.close();
    }
    
    // ################################## FONCTIONS GENERIQUES ##################################
    public void tracerTrait (int x1, int y1, int x2, int y2, String couleur) {
        out.println("  <line x1=\""+x1+"\" y1=\""+y1+"\" x2=\""+x2+"\" y2=\""+y2+"\" stroke=\""+couleur+"\" />");
    }
    
    public void tracerRectangle (int x, int y, int width, int height, String couleur) {
        out.println("  <rect width=\""+width+"\" height=\""+height+"\" x=\""+x+"\" y=\""+y+"\" fill=\""+couleur+"\" />");

    }
    public void tracerFleche (int x1, int y1, int x2, int y2, String couleur) {
        out.println("  <path d=\"M "+x1+" "+y1+" L "+x2+" "+y2+" \" style=\"fill:none;stroke:"+couleur+"; stroke-width:1; marker-end:url(#Triangle)\" />");
    }
    public void ecrireTexte (int x, int y, String texte, String couleur) {
        out.println("  <text x=\""+x+"\" y=\""+y+"\">"+texte+"</text>");
    }
    

    // ################################## FONCTIONS PROCESSUS ##################################
    public void dessinerProcessus(int num, String nom) {
        Processus process = new Processus(num, nom, listeProcessus.size());
        listeProcessus.put(num, process);
        int y = posYFromNumProcess(num);
        tracerTrait(POS_X_PROCESS_START, y, WIDTH, y, "black");
        ecrireTexte(10, y+3, nom, "black");
    }
    
    // Section critique
    public void dessinerSC(int num, int dateDeb, int dateFin) {
        int x = posXFromDate(dateDeb);
        int y = posYFromNumProcess(num) - 15;
        int width = posXFromDate(dateFin) - x;
        int height = 15;
        tracerRectangle(x, y, width, height, "red");
    }
    
    // Action locale
    public void dessinerAL(int num, int date) {
        int x = posXFromDate(date) - 2;
        int y = posYFromNumProcess(num) - 5;
        int width = 4;
        int height = 10;
        tracerRectangle(x, y, width, height, "blue");
    }
    
    public void dessinerMSG(int numDep, int numArr, int dateDeb, int dateFin) {
        dessinerLink(numDep, numArr, dateDeb, dateFin, "grey");
    }
    
    public void dessinerREQ(int numDep, int numArr, int dateDeb, int dateFin) {
        dessinerLink(numDep, numArr, dateDeb, dateFin, "red");
    }
    
    public void dessinerREP (int numDep, int numArr, int dateDeb, int dateFin) {
        dessinerLink(numDep, numArr, dateDeb, dateFin, "blue");
    }
    
    public void dessinerREAL (int numDep, int numArr, int dateDeb, int dateFin) {
        dessinerLink(numDep, numArr, dateDeb, dateFin, "green");
    }
    
    // ################################## FONCTIONS PRIVATE ##################################
    protected int posXFromDate(int date) {
        return POS_X_PROCESS_START + date * SIZE_TIME_UNIT;
    }
    
    protected int posYFromNumProcess(int num) {
        return listeProcessus.get(num).getIndex() * 50 + 50;
    }
    
    protected void dessinerLink(int numDep, int numArr, int dateDeb, int dateFin, String couleur) {
        int x1 = posXFromDate(dateDeb);
        int y1 = posYFromNumProcess(numDep);
        int x2 = posXFromDate(dateFin);
        int y2 = posYFromNumProcess(numArr);
        tracerFleche(x1, y1, x2, y2, couleur);
        ecrireTexte(x1-3, y1-3, String.valueOf(dateDeb), "black");
        ecrireTexte(x2-3, y2-3, String.valueOf(dateFin), "black");
    }
}
